package org.wyn.mongodb.dao.impl;

import java.util.List;

import org.bson.types.ObjectId;
import org.wyn.mongodb.bean.BecomeFriendRequest;
import org.wyn.mongodb.common.SearchBDProvider;

import com.google.code.morphia.dao.BasicDAO;
/**
 * 好友请求mongodb dao层实现
 * @author wyn
 *
 */
public class BecomeFriendRequestDao extends BasicDAO<BecomeFriendRequest,ObjectId>{
	public BecomeFriendRequestDao(){
		super(SearchBDProvider.getInstance());;
	}
	/**
	 * 创建一个请求
	 * @param fromUser
	 * @param toUser
	 * @return
	 */
	public boolean createRequest(String fromUser,String toUser){
		if(0 == createQuery().filter("fromUser", fromUser).filter("toUser", toUser).countAll()//防止重复创建
			&& (!fromUser.equals(toUser))){//自己不能向自己发请求
			BecomeFriendRequest bfr = new BecomeFriendRequest();
			bfr.setFromUser(fromUser);
			bfr.setToUser(toUser);
			bfr.setCreateTime(System.currentTimeMillis());
			save(bfr);
			ensureIndexes();
			return true;
		}
		return false;
	}
	/**
	 * 查看发送到某用户的好友请求
	 * @param toUser
	 * @return
	 */
	public List<BecomeFriendRequest> viewFriendRequestByToUser(String toUser){
		return createQuery().filter("toUser", toUser).asList();
	}
}
